package walkera.cn.customView.colorpicker;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatDialog;

import com.wk.smarthome.R;

import java.util.Locale;

public class ColorPickerDialog   extends  AppCompatDialog  {

    private ColorPickerView mColorPicker;
    private ColorPickerPanelView mNewColor;
    private EditText mHexVal;
    private ColorStateList mHexDefaultTextColor;
    private OnColorChangedListener mListener;


    public ColorPickerDialog(Context context, int initialColor ,OnColorChangedListener  onColorChangedListener ) {
        super(context);
        this.mListener= onColorChangedListener ;
        init(initialColor);
    }

    private void init(int color) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mLayout = inflater.inflate(R.layout.dialog_color_picker, null);

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(mLayout);
        setCanceledOnTouchOutside(true);


        mColorPicker =  mLayout.findViewById(R.id.color_picker_view);
        mNewColor =  mLayout.findViewById(R.id.new_color_panel);

        mHexVal =  mLayout.findViewById(R.id.hex_val);
        mHexVal.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        mHexDefaultTextColor = mHexVal.getTextColors();

        mHexVal.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    String s = mHexVal.getText().toString();

                    try {
                        int c = WkColorTool.convertToColorInt(s );
                        mColorPicker.setColor(c, true);
                        mHexVal.setTextColor(mHexDefaultTextColor);
                    } catch (IllegalArgumentException e) {
                        mHexVal.setTextColor(Color.RED);
                    }

                    return true;
                }
                return false;
            }
        });



        mNewColor.setOnClickListener(onClickListener);
        mColorPicker.setOnColorChangedListener(onColorChangedListener);
        mColorPicker.setColor(color, true);

    }


    private void updateHexValue(int color) {
        if (getAlphaSliderVisible()) {
            mHexVal.setText(WkColorTool.convertToARGB(color).toUpperCase(Locale.getDefault()));
        } else {
            mHexVal.setText(WkColorTool.convertToRGB(color).toUpperCase(Locale.getDefault()));
        }
        mHexVal.setTextColor(mHexDefaultTextColor);
    }



    public boolean getAlphaSliderVisible() {
        return mColorPicker.getAlphaSliderVisible();
    }


    public int getColor() {
        return mColorPicker.getColor();
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.new_color_panel) {
                if (mListener != null) {
                    mListener.onColorChanged(mNewColor.getColor());
                }
            }
            dismiss();
        }
    };

    private ColorPickerView.OnColorChangedListener onColorChangedListener =
            new ColorPickerView.OnColorChangedListener() {
        @Override
        public void onColorChanged(int color) {
            mNewColor.setColor(color);
            updateHexValue(color);
            // 不能频繁回调，否则树莓派性能处理不过来，会延时
            //mListener.onColorChanged(color);

        }
    } ;

    public interface OnColorChangedListener {
        public void onColorChanged(int color);
    }
}
